package cn.springcloud.fix.gateway.bo;

import cn.springcloud.fix.common.core.utils.StringUtil;
import cn.springcloud.fix.gateway.dao.entity.GatewayRoute;
import com.alibaba.fastjson.JSON;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition;

import java.io.Serializable;
import java.net.URI;
import java.util.List;

public class RouteDefinitionBO extends RouteDefinition implements Serializable {

    /**
     * 路由名称
     */
    private String routeName;

    public String getRouteName() {
        return routeName;
    }

    public void setRouteName(String routeName) {
        this.routeName = routeName;
    }

    public static RouteDefinitionBO assembleRouteDefinitionBO(GatewayRoute gatewayRoute) {
        RouteDefinitionBO routeDefinition = new RouteDefinitionBO();
        routeDefinition.setId(gatewayRoute.getRouteId());
        routeDefinition.setRouteName(gatewayRoute.getRouteName());
        routeDefinition.setOrder(gatewayRoute.getRouteOrder());
        routeDefinition.setUri(URI.create(gatewayRoute.getRouteUri()));
        //获取过滤器集合
        if (StringUtil.isNotEmpty(gatewayRoute.getFilters())) {
            List<FilterDefinition> definitions = JSON.parseArray(gatewayRoute.getFilters(), FilterDefinition.class);
            routeDefinition.setFilters(definitions);
        }
        //获取断言集合
        if (StringUtil.isNotEmpty(gatewayRoute.getPredicates())) {
            List<PredicateDefinition> definitions = JSON.parseArray(gatewayRoute.getPredicates(), PredicateDefinition.class);
            routeDefinition.setPredicates(definitions);
        }
        return routeDefinition;
    }
}
